package com.trainoo;

import com.trainoo.service.BaseElasticService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/** 
 * <p> description here </p>
 * @author tao.zhou
 * @date 2020/6/15 15:04:00
 * @version 1.0
 */
@Slf4j
@SpringBootApplication
public class ElasticApplication {

    public static void main(String[] args) {
        SpringApplication.run(ElasticApplication.class, args);
    }

    @RestController
    static class TestController {

        @Autowired
        private BaseElasticService baseElasticService;

        @GetMapping("/check")
        public String checkExist() throws Exception {
            String indexName = "test_elastic";
            baseElasticService.delIndex(indexName);

            boolean exist = baseElasticService.checkIndex(indexName);
            log.info("exist={}", exist);

            if (!exist){
                baseElasticService.index(indexName);
            }

            baseElasticService.bluk(indexName);

            baseElasticService.searchIndex(indexName);

            return "200";
        }

        @GetMapping("/search")
        public String search() throws Exception {
            String indexName = "test_elastic";
            baseElasticService.searchIndex(indexName);
            return "200";
        }
    }
}
